/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cheapflix.controller;

import com.cheapflix.beans.DVD;
import com.cheapflix.beans.User;
import com.cheapflix.dao.DVDDao;
import com.cheapflix.dao.RecommendedDAO;
import com.cheapflix.dao.UserDVDDao;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller class in charge of ratings/recommendations functionalities.
 * @author Group B
 */
@Controller
public class RatingRecommendationController {

    private static final Logger logger = Logger.getLogger(RatingRecommendationController.class);
    private UserDVDDao udvd;
    private RecommendedDAO rdao;
    private DVDDao ddao;
    @Autowired
    public void init(UserDVDDao udvd, RecommendedDAO rdao, DVDDao ddao) {
        this.udvd = udvd;
        this.rdao = rdao;
        this.ddao=ddao;
    }

    @RequestMapping("/rate")
    public ModelAndView rate(@RequestParam("dvdid") int dvdId, @RequestParam("rating") int rating, @RequestParam("comment") String comment,
            HttpSession session) {
        logger.info("Rate called");
       
        User me = (User) session.getAttribute("me");
        if(me==null){
            return new ModelAndView("redirect:/");
        }
        boolean success = udvd.rateDVD(me.getId(), dvdId, rating, comment);
        //refresh user in session
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("redirect:/searchId?id=" + dvdId);
        return mav;
    }

    @RequestMapping("/recommend")
    public ModelAndView recommend(@RequestParam("param") String[] param, @RequestParam("dvdid") String dvdid, HttpServletRequest request,
            HttpSession session) {
        logger.info("Recommend called");
        User me = (User) session.getAttribute("me");
        if(me==null){
            return new ModelAndView("redirect:/");
        }
        ModelAndView mav = new ModelAndView();
        for (String p : param) {

            int friendId = Integer.parseInt(p);
            if (request.getParameter(friendId + "") != null && request.getParameter(friendId + "").equals("on")) {
                rdao.recommendDVDToUser(me.getId(), Integer.parseInt(dvdid), friendId);
                me.addRecommendedDVD(Integer.parseInt(dvdid), friendId);
            }

        }
        mav.setViewName("redirect:/searchId?id=" + dvdid);
        return mav;

    }
    
     @RequestMapping("/recommenddetails")
    public ModelAndView recommenddetails ( @RequestParam("dvdid")int dvdid,   HttpSession session){
        User me = (User)session.getAttribute("me");
        if(me==null){
            return new ModelAndView("redirect:/");
        }
        List<User> users=rdao.getFriendsWhoRecommendedFlickToMe ( me.getId(),dvdid );
        DVD dvd = ddao.getDVD(dvdid);
        ModelAndView mav= new ModelAndView();
        mav.addObject("users",users);
        mav.addObject("dvd",dvd);
        mav.setViewName("RecommendationDetails");
        return mav;
    }
}
